///////////////////////////////////////////////////////////////////
//                            _ooOoo_                            //
//                           o8888888o                           //
//                           88" . "88                           //
//                           (| -_- |)                           //
//                            O\ = /O                            //
//                        ____/`---'\____                        //
//                      .   ' \\| |// `.                         //
//                       / \\||| : |||// \                       //
//                     / _||||| -:- |||||- \                     //
//                       | | \\\ - /// | |                       //
//                     | \_| ''\---/'' | |                       //
//                      \ .-\__ `-` ___/-. /                     //
//                   ___`. .' /--.--\ `. . __                    //
//                ."" '< `.___\_<|>_/___.' >'"".                 //
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |              //
//                 \ \ `-. \_ __\ /__ _/ .-` / /                 //
//         ======`-.____`-.___\_____/___.-`____.-'======         //
//                            `=---='                            //
//                                                               //
//         .............................................         //
//                  佛祖保佑             永无BUG                  //
//                  需求不变             永不修改                 //
///////////////////////////////////////////////////////////////////
package com.longyue.ms.common.interceptors;

import com.alibaba.fastjson.JSON;

import com.longyue.ms.common.anotation.LoginCheck;
import com.longyue.ms.common.bean.ConstantBean;
import com.longyue.ms.common.util.ContextUtil;
import com.longyue.ms.common.util.RequestUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author: WenWenLi
 * @description: 登录校验拦截器
 * @date: 2018-01-19
 * @Version: 1.0
 **/
public class LoginInterceptors extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod target = (HandlerMethod) handler;
            if (hasLoginCheckAnnoation(target)) {
                if(ContextUtil.hasLogin()){
                    return true;
                }else{
                     if(RequestUtils.isAjaxRequest(request)){
                         response.setCharacterEncoding("UTF-8");
                         response.setContentType("application/json; charset=utf-8");
                         PrintWriter out = null;
                         try {
                             out = response.getWriter();
                             out.append(JSON.toJSONString(ConstantBean.NEED_LOGIN));
                         } catch (Exception e) {
                             e.printStackTrace();
                         } finally {
                             if (out != null) {
                                 out.close();
                             }
                         }
                         return false;
                     }else{
                         response.sendRedirect("/");
                         return true;
                     }

                }

            } else {
                return true;
            }
        } else {
            return super.preHandle(request, response, handler);

        }
    }

    private boolean hasLoginCheckAnnoation(HandlerMethod method) {
        LoginCheck methodCheck = method.getMethod().getAnnotation(LoginCheck.class);
        LoginCheck clazzCheck = method.getBeanType().getAnnotation(LoginCheck.class);
        return (methodCheck == null && clazzCheck == null) ? false : true;
    }
}
